<?php
/**
 * Created by PhpStorm.
 * User: 2016-08-23
 * Date: 2017/11/6
 * Time: 10:46
 */
$api_service_path = __DIR__ . "/../../../../api/";
ob_start();
require_once($api_service_path . "public/index.php");
ob_end_clean();
DB::connection()->disableQueryLog(); 

/**
 * 用于每日统计 营收的金额
 */

DB::connection()->disableQueryLog(); 
$mail = array(
    'yuanjie@anhuibj.com',
    'husheng@anhuibj.com',
    'jinxin@anhuibj.com','ahljt@anhuibj.com',
);                
                

$jihua=array(
    1  =>array( 93 ,85 ,93 ,110 ,104 ,90 ,110 ,100 ,111 ,89 ,100 ,106  ),                                                           
    3  =>array( 92 ,54 ,97 ,99 ,106 ,103 ,104 ,97 ,117 ,106 ,96 ,109  ),            
    8  =>array( 41 ,40 ,50 ,53 ,52 ,48 ,55 ,48 ,51 ,46 ,52 ,51  ),       
    7  =>array( 80 ,58 ,85 ,85 ,94 ,85 ,80 ,80 ,90 ,90 ,90 ,90  ),       
    4  =>array( 95 ,55 ,96 ,97 ,98 ,98 ,103 ,103 ,103 ,98 ,106 ,98 ),           
    6  =>array( 59 ,36 ,60 ,67 ,65 ,65 ,68 ,79 ,78 ,85 ,80 ,85 ),       
    11 =>array( 20 ,20 ,26 ,29 ,30 ,30 ,30 ,31 ,32 ,32 ,32 ,33                                                             ),       
    10 =>array( 14 ,10 ,15 ,15 ,16 ,19.0960 ,20.0320 ,20.9880 ,21.9640 ,22.9600 ,23.9760 ,24.6420                    ),             
    9  =>array( 12 ,12 ,14 ,15 ,16.5000 ,18 ,19 ,22 ,22 ,22 ,21 ,19                                                       ),        
    12 =>array( 10 ,8 ,11 ,10 ,11 ,14.7420 ,16 ,18 ,19 ,20 ,22 ,23                                                       ),         
    14 =>array( 7.1000 ,6 ,10 ,11.6000 ,8.5000 ,13.9150 ,14.7280 ,15.6750 ,17.1100 ,18.5850 ,20.4350 ,22.3650      ),               
    15 =>array( 7 ,4 ,7.5000 ,8.5000 ,6.5000 ,11.5020 ,12.8150 ,14.4210 ,15.8340 ,17.2870 ,18.7800 ,20.3130        ),               
    13 =>array( 3 ,3 ,5 ,6 ,8 ,8 ,10 ,8 ,9 ,8 ,9 ,10                                                             ),                 
    16 =>array( 7 ,5 ,7 ,10 ,10 ,12 ,13.4200 ,14.8800 ,16.1200 ,18.2000 ,19.5000 ,20.8000                          ),               
    17 =>array( 8 ,6 ,9 ,9 ,8 ,8 ,9.0200 ,10.0800 ,11.1800 ,12.6000 ,13.5000 ,14.4000                            ),    
);

$date=time()-86400;
// $date=strtotime('2016-12-25');
$mdb=MaintenanceShops::where('status',1)->get();
$finally=array();
$mounth=date('Y-m-01 00:00:00',$date);//月初 

$begin_date=date('Y-m-d 00:00:00',$date);
$end_date=date('Y-m-d 23:59:59',$date);
foreach ($mdb as $_mdb) {//计算每个点的营业额
	$shop_id=$_mdb->id;
	
	$order_day_amount = Orders::whereIn('orders.status',array(1018,1017,1217,1317,1417))
            ->whereBetween('orders.pay_time',array($begin_date,$end_date))
            ->where('orders.shop_id',$shop_id)
            ->where('orders.place',2)->sum("payable_amount");
    $order_mon_amount = Orders::whereIn('orders.status',array(1018,1017,1217,1317,1417))
            ->whereBetween('orders.pay_time',array($mounth,$end_date))
            ->where('orders.shop_id',$shop_id)
            ->where('orders.place',2)->sum("payable_amount"); 

    $wash_day_amount = CarWashOrder::where('status',1417)
            ->whereBetween('pay_time',array($begin_date,$end_date))
            ->where('shop_id',$shop_id)->sum("pay_amount"); 
    $wash_mon_amount = CarWashOrder::where('status',1417)
            ->whereBetween('pay_time',array($mounth,$end_date))
            ->where('shop_id',$shop_id)->sum("pay_amount"); 

    $scorder_day_amount = ServiceCardOrder::whereIn('service_card_order.status',array(1018,1017,1217,1317,1417))
            ->whereBetween('service_card_order.pay_time',array($begin_date,$end_date))
            ->where('service_card_order.shop_id',$shop_id)->sum("pay_amount"); 
    $scorder_mon_amount = ServiceCardOrder::whereIn('service_card_order.status',array(1018,1017,1217,1317,1417))
            ->whereBetween('service_card_order.pay_time',array($mounth,$end_date))
            ->where('service_card_order.shop_id',$shop_id)->sum("pay_amount"); 

    $returnorder_day_amount=ReturnOrder::leftJoin("orders","orders.order_id","=","return_order.order_id")
            ->where("return_order.status",1)->where("orders.shop_id",$shop_id)->where("orders.place",2)->whereBetween("return_order.implement_time",array($begin_date,$end_date))->sum("return_order.amount"); 
    $returnorder_mon_amount=ReturnOrder::leftJoin("orders","orders.order_id","=","return_order.order_id")
            ->where("return_order.status",1)->where("orders.shop_id",$shop_id)->where("orders.place",2)->whereBetween("return_order.implement_time",array($mounth,$end_date))->sum("return_order.amount"); 

    $returnwash_day_amount = ReturnCarwashOrder::leftJoin("car_wash_order","car_wash_order.order_id","=","return_carwash_order.order_id")
            ->where('return_carwash_order.status',1)
            ->whereBetween('return_carwash_order.implement_time',array($begin_date,$end_date))
            ->where('car_wash_order.shop_id',$shop_id)->sum("car_wash_order.pay_amount"); 
    $returnwash_mon_amount = ReturnCarwashOrder::leftJoin("car_wash_order","car_wash_order.order_id","=","return_carwash_order.order_id")
            ->where('return_carwash_order.status',1)
            ->whereBetween('return_carwash_order.implement_time',array($mounth,$end_date))
            ->where('car_wash_order.shop_id',$shop_id)->sum("car_wash_order.pay_amount"); 

    $returnscorder_day_amount = ReturnServiceCarOrder::leftJoin("service_card_order","service_card_order.order_id","=","return_service_card_order.order_id")
            ->where('return_service_card_order.status',1)
            ->whereBetween('return_service_card_order.implement_time',array($begin_date,$end_date))
            ->where('service_card_order.shop_id',$shop_id)->sum("return_service_card_order.amount"); 
    $returnscorder_mon_amount = ReturnServiceCarOrder::leftJoin("service_card_order","service_card_order.order_id","=","return_service_card_order.order_id")
            ->where('return_service_card_order.status',1)
            ->whereBetween('return_service_card_order.implement_time',array($mounth,$end_date))
            ->where('service_card_order.shop_id',$shop_id)->sum("return_service_card_order.amount");
    $chengben=ReportDateReocrd::getDate($mounth,$end_date,$shop_id,"get_profit_report");//获取一个月的毛利率分析
    $chengben_amount=0;
    foreach ($chengben as $cb) {
        $chengben_amount+=round($cb['item_amount']/100,2);
    }
    unset($chengben);
    $day_amount=round(($order_day_amount+$wash_day_amount+$scorder_day_amount-$returnorder_day_amount-$returnwash_day_amount-$returnscorder_day_amount)/100,2);
    $mon_amount=round(($order_mon_amount+$wash_mon_amount+$scorder_mon_amount-$returnorder_mon_amount-$returnwash_mon_amount-$returnscorder_mon_amount)/100,2);
    $jihu=isset($jihua[$shop_id][(date('m',$date)-1)])?$jihua[$shop_id][(date('m',$date)-1)]:0;

    $finally[]=array(
    	"shop_name"=>$_mdb->name, 
    	"day_amount"=>$day_amount,
    	"mon_amount"=>$mon_amount,
        "chengben_amount"=>$chengben_amount,
    	"jihu"=>$jihu,
	);
	
	Log::info("门店".$_mdb->id." 日期 ".date('Y-m-d',$date).'完毕');
	echo "门店".$_mdb->id." 日期 ".date('Y-m-d',$date).'完毕';
}
//当天的退库
$now_out_array=array();
$out_list = DB::select("SELECT  maintenance_out_list.consuming_id,sum(moi.tax_purchase_subtotal) as amount
    FROM
        maintenance_out_item moi
    LEFT JOIN maintenance_out_list ON moi.list_id = maintenance_out_list.id
    LEFT JOIN maintenance_storage_item msi ON msi.id = moi.storage_id
    LEFT JOIN maintenance_storage_list msl ON msl.id = moi.storage_list_id
    WHERE
        maintenance_out_list.implement_time BETWEEN ?
    AND ?
    AND `maintenance_out_list`.`status` = 1
    AND `moi`.`status` = 1
    AND `maintenance_out_list`.`type` = 2
    AND `maintenance_out_list`.`out_type` = 2
    GROUP BY maintenance_out_list.consuming_id", array($begin_date, $end_date));
foreach ($out_list as $_v) {
    $now_out_array[$_v->consuming_id]=-$_v->amount;
}
//当天的入库
$now_storage=array();
$now_storage_list = DB::select("SELECT  maintenance_storage_list.supplier,
    maintenance_supplier.name supplier_name,sum(msi.tax_purchase_subtotal) as amount
    FROM
        maintenance_storage_item msi
    LEFT JOIN maintenance_storage_list ON msi.list_id = maintenance_storage_list.id
    LEFT JOIN maintenance_supplier ON maintenance_supplier.id = maintenance_storage_list.supplier
    WHERE
        `maintenance_storage_list`.`implement_time` BETWEEN ?
    AND ?
    AND `maintenance_storage_list`.`status` = 1
    AND `msi`.`status` = 1
    AND `maintenance_storage_list`.`type` =1
    GROUP BY maintenance_storage_list.supplier
    ORDER BY
        amount desc", array($begin_date, $end_date));
foreach ($now_storage_list as $_v) {
    $temp_out=0;
    if(isset($now_out_array[$_v->supplier])){
        $temp_out=$now_out_array[$_v->supplier];
        unset($now_out_array[$_v->supplier]);
    }
    $now_storage_list[$_v->supplier]=$_v->amount+$temp_out;
}
foreach ($now_out_array as $key=>$_v) {
    $now_storage_list[$key]=$_v;
}
unset($now_out_array);
//全月的的退库
$mon_out_array=array();
$mon_list = DB::select("SELECT  maintenance_out_list.consuming_id,sum(moi.tax_purchase_subtotal) as amount
    FROM
        maintenance_out_item moi
    LEFT JOIN maintenance_out_list ON moi.list_id = maintenance_out_list.id
    LEFT JOIN maintenance_storage_item msi ON msi.id = moi.storage_id
    LEFT JOIN maintenance_storage_list msl ON msl.id = moi.storage_list_id
    WHERE
        maintenance_out_list.implement_time BETWEEN ?
    AND ?
    AND `maintenance_out_list`.`status` = 1
    AND `moi`.`status` = 1
    AND `maintenance_out_list`.`type` = 2
    AND `maintenance_out_list`.`out_type` = 2
    GROUP BY maintenance_out_list.consuming_id", array($mounth, $end_date));
foreach ($mon_list as $_v) {
    $mon_out_array[$_v->consuming_id]=-$_v->amount;
}
//全月的 
$mount_storage=array();
$storage_list = DB::select("SELECT  maintenance_storage_list.supplier,
    maintenance_supplier.name supplier_name,sum(msi.tax_purchase_subtotal) as amount
    FROM
        maintenance_storage_item msi
    LEFT JOIN maintenance_storage_list ON msi.list_id = maintenance_storage_list.id
    LEFT JOIN maintenance_supplier ON maintenance_supplier.id = maintenance_storage_list.supplier
    WHERE
        `maintenance_storage_list`.`implement_time` BETWEEN ?
    AND ?
    AND `maintenance_storage_list`.`status` = 1
    AND `msi`.`status` = 1
    AND `maintenance_storage_list`.`type` =1
    GROUP BY maintenance_storage_list.supplier
    ORDER BY
        amount desc", array($mounth, $end_date));
$sort=array();
foreach ($storage_list as $_v) {
    $temp_out=0;
    if(isset($mon_out_array[$_v->supplier])){
        $temp_out=$mon_out_array[$_v->supplier];
        unset($mon_out_array[$_v->supplier]);
    }
    $day_amount=0;
    if(isset($now_storage_list[$_v->supplier])){
        $day_amount=$now_storage_list[$_v->supplier];
        unset($now_storage_list[$_v->supplier]);
    }
    $mount_storage[$_v->supplier]=array("name"=>$_v->supplier_name,"mon_amount"=>($_v->amount+$temp_out),"day_amount"=>$day_amount);
    $sort[$_v->supplier]=($_v->amount+$temp_out);
}
array_multisort($sort, SORT_DESC, $mount_storage);
$finally_storage=array();
foreach ($mount_storage as $value) {
    $finally_storage[]=$value;
    if(sizeof($finally_storage)==20){
        break;
    }
}

$all=array("order"=>$finally,"storage"=>$finally_storage);

$data_arr = array(
    'mail' => $mail,
    'report' => $all,
    "date"=>$date,
);
if(sizeof($finally)>0){
	Mail::send('emails.daily_recivable', $data_arr, function($message)use ($data_arr)
	{
	    $message->to($data_arr['mail'], date('Y年m月d日',$data_arr['date']).'门店销售额-采购')->subject(date('Y年m月d日',$data_arr['date']).'门店销售额-采购');
	});
}